package com.basic.library.binding.viewadapter.recyclerview;

import android.util.DisplayMetrics;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import com.basic.library.brvah.FlowLayoutManager;

import me.tatarka.bindingcollectionadapter2.LayoutManagers;

/**
 * @author yj
 * @description
 */
public class MLayoutManagers extends LayoutManagers {

    public static LayoutManagerFactory mLinear() {
        return new LayoutManagerFactory() {
            @Override
            public RecyclerView.LayoutManager create(RecyclerView recyclerView) {
                LinearLayoutManager layoutManager = new LinearLayoutManager(recyclerView.getContext()) {
                    @Override
                    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                        return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT);
                    }
                };
                return layoutManager;
            }
        };
    }

    public static LayoutManagerFactory wrapLinear() {
        return new LayoutManagerFactory() {
            @Override
            public RecyclerView.LayoutManager create(RecyclerView recyclerView) {
                LinearLayoutManager l = new LinearLayoutManager(recyclerView.getContext());
                l.setAutoMeasureEnabled(true);
                return l;
//                return new WrappingLinearLayoutManager(recyclerView.getContext(), LinearLayoutManager.VERTICAL, false);
            }
        };
    }

    /**
     * 控制滚动速度
     *
     * @return
     */
    public static LayoutManagerFactory mLinear1() {
        return new LayoutManagerFactory() {
            @Override
            public RecyclerView.LayoutManager create(RecyclerView recyclerView) {
                LinearLayoutManager layoutManager = new LinearLayoutManager(recyclerView.getContext()) {
                    @Override
                    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
                        LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
                            @Override
                            protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                                //这边可以自定义进行控制速度
                                float d = 1f;
                                if (displayMetrics != null)
                                    d = displayMetrics.density; //1.5
                                return 3f / d;
                            }
                        };
                        linearSmoothScroller.setTargetPosition(position);
                        startSmoothScroll(linearSmoothScroller);
                    }
                };
                return layoutManager;
            }
        };
    }

    public static LayoutManagerFactory flow() {
        return new LayoutManagerFactory() {
            @Override
            public RecyclerView.LayoutManager create(RecyclerView recyclerView) {
                return new FlowLayoutManager();
            }
        };
    }
}
